package com.bloveoacom.base;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.MotionEvent;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.bloveoacom.delegate.IActivity;
import com.bloveoacom.util.ActivityUtil;
import com.bloveoacom.util.KeyboardUtil;
import com.bloveoacom.util.LoadDialogUtil;
import com.bloveoacom.util.TUtil;
import com.bloveoacom.exception.ApiException;
import com.bloveoacom.mvp.BasePresenter;
import com.bloveoacom.mvp.IModel;
import com.bloveoacom.mvp.IView;
import com.bloveoacom.weichi.R;
import com.bloveoacom.weichi.activity.LoginActivity;
import com.gyf.barlibrary.ImmersionBar;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

/**
 * author: yjl
 * e-mail: 1271901673@qq.com
 * time  : 2018/9/20
 * desc  :
 */
public abstract class BaseActivity<P extends BasePresenter, M extends IModel> extends MySupportActivity implements IActivity, IView {

    protected Context mContext;
    protected P mPresenter;
    protected M mModel;
    protected ImmersionBar immersionBar;
    private Unbinder unbinder;
    private CompositeDisposable mCompositeDisposable;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getLayoutResId() != 0) {
            setContentView(getLayoutResId());
            unbinder = ButterKnife.bind(this);
        }
        mContext = this;
        Bundle extras = getIntent().getExtras();
        if (null != extras) {
            getBundleExtras(extras);
        }
        try {
            mPresenter = TUtil.getT(this, 0);
            mModel = TUtil.getT(this, 1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (mPresenter != null) {
            mPresenter.onAttached(this, mModel);
        }
        if (isSetStatusBarAlpha()) {
            immersionBar = ImmersionBar.with(this);
            immersionBar.keyboardEnable(true)
                    .statusBarDarkFont(isDarkMode(), 0.5f);
            if(isSetBarColor()){
                //immersionBar.barColor(R.color.color_bg);
            }
            immersionBar.init();
            setTitilBar();
        }
        initView();
        initData();
        initListener();
    }

    public boolean isSetBarColor(){
        return true;
    }


    @Override
    public void onDestroy() {
        hideLoadingDialog();
        super.onDestroy();
        if (unbinder != null) {
            unbinder.unbind();
        }
        if (mPresenter != null) {
            mPresenter.onDestroy();
        }
        if (immersionBar != null) {
            immersionBar.destroy();
        }
        unDispose();
    }
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        KeyboardUtil.handleAutoCloseKeyboard(isAutoCloseKeyboard(), getCurrentFocus(), ev, this);
        return super.dispatchTouchEvent(ev);
    }
    protected boolean isAutoCloseKeyboard() {
        return true;
    }

    protected void unDispose() {
        if (mCompositeDisposable != null && !mCompositeDisposable.isDisposed()) {
            mCompositeDisposable.dispose();
        }
    }
    protected void addDispose(Disposable disposable) {
        if (mCompositeDisposable == null) {
            mCompositeDisposable = new CompositeDisposable();
        }
        mCompositeDisposable.add(disposable);
    }


    private void setTitilBar(){
//        if(titleBar!=null){
//            ImmersionBar.setTitleBar(mContext, titleBar);
//        }
        if(getViewSetTB()!=null){
            ImmersionBar.setTitleBar(this, getViewSetTB());
        }

    }
    protected View getViewSetTB(){
        return null;
    }

    protected boolean isSetStatusBarAlpha() {
        return true;
    }

    /**
     * 是否把状态栏字体及图标颜色设置为深色
     *
     * @return
     */
    protected boolean isDarkMode() {
        return true;
    }

    //----------------------------提示------------------------------
    @Override
    public void showLoadingDialog(String title) {
        LoadDialogUtil.showWaitDialog(mContext,title);
    }

    @Override
    public void showLoadingDialog() {
        LoadDialogUtil.showWaitDialog(mContext);
    }

    @Override
    public void showLoadingDialog(String title, boolean isCancel) {
        LoadDialogUtil.showWaitDialog(mContext,title,isCancel);
    }

    @Override
    public void hideLoadingDialog() {
        LoadDialogUtil.dismissWaitDialog();
    }

    @Override
    public void showMessage(String message) {
        ToastUtils.showShort(message);
    }

    @Override
    public void showMessage(int resId) {
        ToastUtils.showShort(resId);
    }

    @Override
    public void showErrorException(ApiException e) {
        ToastUtils.showShort(e.getMessage());
    }

    @Override
    public void finishRefresh() {

    }

    @Override
    public void showRefreshLoading() {

    }
    protected boolean isLogin() {
        if (UserInfoBean.getInstance().isLogin()) {
            return true;
        } else {
            ActivityUtil.startActivity(mContext,LoginActivity.class);
            return false;
        }
    }
}
